package stealth.novaposhta;

import stealth.novaposhta.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class TrackView extends Activity {
    Resources res;
    EditText textbox;
    private PowerManager.WakeLock wl;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "DoNotDimScreen");
        setContentView(R.layout.track_view);
        res = getResources();
        textbox = (EditText) findViewById(R.id.track_textbox);
        Uri uri = getIntent().getData();
        if (uri != null) {
            String ttn = uri.getSchemeSpecificPart();
            textbox.setText(ttn);
            displayTracking(ttn);
        } else {
            textbox.setOnEditorActionListener(new OnEditorActionListener() {
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    if (actionId == EditorInfo.IME_NULL || actionId == EditorInfo.IME_ACTION_GO) {
                        displayTracking(v.getText().toString());
                        return true;
                    }
                    return false;
                }
            });
        }
    }
    
    @Override
    protected void onPause() {
            super.onPause();
            wl.release();
    }

    @Override
    protected void onResume() {
            super.onResume();
            wl.acquire();
    }
    
    private void displayTracking(String ttn) {
        final String tracking = ttn;
        final ProgressDialog progress = ProgressDialog.show(TrackView.this, "", res.getString(R.string.loading));
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(textbox.getWindowToken(), 0);
        final Handler handler = new Handler() {
           public void handleMessage(Message msg) {
               progress.dismiss();
           }
        };
        Thread getTracking = new Thread() {  
            public void run() {
                WebView webview = (WebView) findViewById(R.id.track_webview);
                String notAvailable = res.getString(R.string.server_unavailable);
                String contents = new NetUtil("http://91.220.203.10/site_services/tracking/tracking.php?en=" + tracking).toString(notAvailable);
                int startIndex = contents.indexOf("</form>");
                if (startIndex != -1) {
                	startIndex = startIndex + "</form>".length();
                    contents = contents.substring(startIndex);
                }
                webview.loadDataWithBaseURL("http://novaposhta.ua/frontend/tracking", "<html><body>" + contents + "</body></html>", "text/html", "UTF-8", null);
                handler.sendEmptyMessage(0);
            }
        };
        getTracking.start();
    }
}